package fps;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class GameScreen extends JPanel{
	private static final long serialVersionUID = 7465367887634785377L;
	
	public int[][][] gameMap = new int[20][20][20];
	public Color groundColor = new Color(0,125,0);
	public Color skyColor = new Color(0,0,255);
	
	public boolean moving = false;
	public double nummy = 1;
	
	public Player Jim;
	public MainGUI mg;

	@SuppressWarnings("unused")
	public GameScreen(Player p, MainGUI m){
		super();
		
		//fills the game map with a random assortment of blocks and non blocks
		for (int[][] i : gameMap){
			for (int[] j : i){
				for (int k : j){
					k = (int) ((Math.random()*2) - 1);
				}
			}
		}
		
		Jim = p;
		mg = m;
	}

	public void updateGame() {
		this.repaint();
		this.paint(this.getGraphics());
		
		if(mg.up){
			Jim.yPos += Math.cos(Jim.dir*(180/Math.PI))*Jim.speed;
			Jim.xPos += Math.sin(Jim.dir*(180/Math.PI))*Jim.speed;
			moving = true;
		}
		if(mg.down){
			Jim.yPos -= Math.cos(Jim.dir*(180/Math.PI))*Jim.speed;
			Jim.xPos -= Math.sin(Jim.dir*(180/Math.PI))*Jim.speed;
		}
		if(mg.left){
			Jim.dir++;
		}
		if(mg.right){
			Jim.dir--;
		}
		
	}
	
	public void paint(Graphics g){
		
		//make buffered image
		BufferedImage B = new BufferedImage(mg.getWidth(), mg.getHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics g2 = B.getGraphics();
		
		//draw ground
		g2.setColor(groundColor);
		g2.fillRect(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
		
		
		
		//draw Lines
		g2.setColor(Color.BLACK);
		int yy = this.getHeight();
		int xx = this.getWidth();
		
		//vertical lines
		int lineSpacing = 5;
		for(int i = -999; i<1000; i++){
			g2.drawLine((int) ((xx)/lineSpacing*i+0*5), yy, (int) (xx/2+0*5), yy/2);
		}
		
		//horizontal lines
		//g2.translate(0, nummy);
		for(double i = 2; i<1000; i+=1){
			g2.drawLine((int)(0), (int)((yy/2) + (yy/i)), xx, (int)((yy/2) + (yy/i)));
		}
		
		g2.setColor(Color.RED);
		g2.fillOval((int) Jim.dir, yy*3/4, 100, 100);
		//System.out.println(this.getHeight()/2);
		//System.out.println(Jim.dir);
		
		
		//remember x=(1/m)y + (x-int)
		//and y=mx + b
		/*
		int x1, x2, y1, y2;
		
		//y = 0x + b
		//
		for(int i = 0; i<1000; i++){
			x1 = 0;
			y1 = 0;
			x2 = 0;
			y2 = 0;
			g2.drawLine(x1,y1,x2,y2);
		}*/
		
		//draw sky
		g2.setColor(skyColor);
		g2.fillRect(0, 0, this.getWidth(), this.getHeight()*35/64);
		
		//draw buffered image image
		g.drawImage(B, 0, 0, null);
		this.repaint();
	}
}
